/*
Do-file name: 10_EventStudy.do
Author: Thiago
Date: Nov 7th 2018
This version: November 2022
Comments: This do-file produces the event-study figure for case disposition across transitions
*/

use "$temp/judges_estimation.dta", clear

qui unique Court_idP,by(Judge_idP) gen(tot_courts)
bys Judge_idP: egen tot_courts_max = max(tot_courts)
gen single_court = tot_courts_max == 1

qui unique Comarca_idP,by(Judge_idP) gen(tot_comarca)
bys Judge_idP: egen tot_comarca_max = max(tot_comarca)
gen single_comarca = tot_comarca_max == 1

gen transition_patterns = .
replace transition_patterns = 0 if single_court == 1
replace transition_patterns = 1 if (single_court == 0 & single_comarca == 1)
replace transition_patterns = 2 if (single_comarca == 0 & promotion_max == 0)
replace transition_patterns = 2 if (single_court == 0 & single_comarca ==0 & promotion_max == .)						//for few judges promotion is missing, b
replace transition_patterns = 3 if (single_court == 0 & single_comarca ==0 & promotion_max == 1)

by Judge_idP: gen transition_patterns_unique = transition_patterns if _n == 1

lab def transition_patterns 0"Never changed Courts" 1"Changed Court but not District" 2"Changed Districts but not promoted" ///
3"Promoted"
lab val transition_patterns transition_patterns
lab val transition_patterns_unique transition_patterns

tab transition_patterns_unique, gen(transition_patterns_unique_)
lab var transition_patterns_unique_1 "Never changed Courts"
lab var transition_patterns_unique_2 "Changed Court but not District"
lab var transition_patterns_unique_3 "Changed Districts but not promoted"
lab var transition_patterns_unique_4 "Promoted"

****** Step 1: Judge has to work in each district for at least 6 months *********
bys Judge_idP YM: gen courts_worked = _N				//keep track of number of courts worked in month before dropping anything

collapse (sum) Sent_Trial_Merits_J Hearing_J (first) Judge_Court_idP comarca courts_worked promotion_max court_level*  , by(Judge_idP Comarca_idP YM )

bys Judge_idP Comarca_idP:  gen number_comarca = _N

keep if number_comarca >= 6								//10,610 unique judges
codebook Judge_idP

******Step 2: only keep judges that work in more than one District (Comarca) (only considering those 6 month matches) ******
bys Judge_idP Comarca_idP:  gen j_comarca = 1 if _n == 1
bys Judge_idP: egen tot_comarca = total(j_comarca)

keep if tot_comarca > 1									//4,824	unique judges	
codebook Judge_idP


****** Step 3: We want clean-cut transitions, not judges who are working at several distinct districts at the same month.******
egen judge_month = group(Judge_idP YM)
qui unique Comarca_idP, by(judge_month) gen(districts_month)
gen multiple_dist = (districts_month > 1) if districts_month != .
bys Judge_idP: egen mean = mean(multiple_dist)

by Judge_idP: gen mean_first = mean if _n == 1

keep if mean <= 0.05									//2,239 unique judges
codebook Judge_idP

****** Step 4: Now determine transitions from one comarca to another ******
sort Judge_idP  YM Comarca_idP
by Judge_idP: gen transition =  comarca[_n] != comarca[_n+1]			//One if comarca changed
by Judge_idP: replace transition = 0 if _n == _N						//last period set to zero, no transition
by Judge_idP: egen total_transition = total(transition)						
by Judge_idP: gen total_transition_first = total_transition if _n==1


keep if total_transition == 1				//For now, only clean-cut transitions

gen time = YM if transition == 1
by Judge_idP: egen time_trans = min(time)						

gen time_relative = YM - time_trans-1
gen post = time_relative>0
gen time_relative_pos = time_relative + 12

collapse (sum) Sent_Trial_Merits_J  Hearing_J (first)  Comarca_idP comarca promotion_max transition time_relative_pos time_relative,  by(Judge_idP YM)

 gen ihs_sentence = asinh(Sent_Trial_Merits_J)
 gen ihs_hearings = asinh(Hearing_J)

// by Judge_idP: gen judge_fe = judge_fe_invsine if _n==1
// sum judge_fe,d
// gen above_median = judge_fe_invsine > r(p50)

keep if (time_relative >= -12 & time_relative <=11)
by Judge_idP: gen number_periods = _N
gen pre = time_relative <0
bys Judge_idP: egen number_pre = sum(pre)
gen number_post = number_periods - number_pre

*Step 4: only keep judges with 3 pre- and post-periods
keep if number_post>=3 & number_pre >= 3


qui reghdfe ihs_sentence io6.time_relative_pos if (time_relative >= -12 & time_relative <= 11), a(YM Judge_idP) vce(cluster Judge_idP)
estimates store model
loc number_full = e(N_clust)


******************************************************************************
**** Figure 3: Event-study around judicial district movement *****
*****************
coefplot model, yline(0) xline(11.5, lp(dot)) nolabels grid(none) drop(_cons 0.time_relative_pos) ///
	coeflabels(1.time_relative_pos = "t-11"	2.time_relative_pos = "t-10"	3.time_relative_pos = "t-9"	///
			   4.time_relative_pos = "t-8"	5.time_relative_pos = "t-7"	6.time_relative_pos = "t-6"	///
			   7.time_relative_pos = "t-5"	8.time_relative_pos = "t-4"	9.time_relative_pos = "t-3"	///
			   10.time_relative_pos = "t-2"	11.time_relative_pos = "t-1"	12.time_relative_pos = "t=0" ///
			   13.time_relative_pos = "t+1"	14.time_relative_pos = "t+2"	15.time_relative_pos = "t+3" ///
			   16.time_relative_pos = "t+4"	17.time_relative_pos = "t+5"	18.time_relative_pos = "t+6" ///
			   19.time_relative_pos = "t+7"	20.time_relative_pos = "t+8"	21.time_relative_pos = "t+9" ///
			   22.time_relative_pos = "t+10"	23.time_relative_pos = "t+11"	24.time_relative_pos = "t+12"	, ///
	labsize(small) angle(90)) vertical  graphregion(fcolor(white)) note(Number of Judges/Transitions = `number_full') ///
	ytitle("IHS Cases Disposed") omitted base
graph export "$out/event_all.pdf", replace
